﻿using System;
using System.Runtime.InteropServices;
/*
 * 创建人：
 * 
 * 时间：
 * 
 * 说明：
 * 
 * 
 * 修改说明：
 * 
 * 
 */


namespace ConsoleTest.ChapterEight
{
    public class PInvoke
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool Beep(uint dwFreq, uint dwDuration);

        /// <summary>
        /// 
        /// </summary>
        public static void UseKernelBeep()
        {
            Console.WriteLine("Testing PC speaker...");
            for (uint i = 100; i <= 20000; i++)
            {
                Beep(i, 5);
            }
            Console.WriteLine("Testing complete.");
        }


        [DllImport("user32.dll",CharSet=CharSet.Auto)]
        static extern int MessageBox(System.IntPtr hWnd,string text,string caption,uint type);
        [DllImport("kernel32", CharSet = CharSet.Auto)]
        static extern uint GetCurrentDirectory(uint size, System.Text.StringBuilder strBuilder);
        [DllImport("kernel32", CharSet = CharSet.Auto)]
        static extern System.IntPtr GetCommandLine();

        /// <summary>
        /// 
        /// </summary>
        public static void UseMsgBox()
        {
            MessageBox(System.IntPtr.Zero,"Hello World","Caption Text",0);
        }

        /// <summary>
        /// 由调用代码指定缓存并告知缓存大小
        /// </summary>
        public static void GetString()
        {
            uint size = 255;
            System.Text.StringBuilder strBuilder = new System.Text.StringBuilder((int)size);
            uint i = GetCurrentDirectory(size, strBuilder);
            System.Console.WriteLine("GetCurrentDirectory() :"+i.ToString());

            System.IntPtr ptr = GetCommandLine();
            string str = System.Runtime.InteropServices.Marshal.PtrToStringAuto(ptr);
            System.Console.WriteLine("GetCommandLine() :"+str);
        }
    }

}